﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM;
using System.Globalization; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class WF_LoanAgreement : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;


        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                BindRadio();

                this.ViewState.Add("IsUserControlPostBack", true);

            }


            if (DataItem != null)
            {
                BindRadio();


                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {


                    BindRadio();
                  //  btnFindEmployee.Visible = false;

                    lblContSeq.Text = DataBinder.Eval(DataItem, "cont_seq").ToString();
                    lblEmpSeq.Text = DataBinder.Eval(DataItem, "emp_seq").ToString();

                    WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();
                    DataTable dt;
                    dt = obj.SelectEmpByID(int.Parse(lblEmpSeq.Text));
                                       
                    txtEmployee.Text = dt.Rows[0]["full_name"].ToString();
                    
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "start_date", "{0:dd/MM/yyyy}").ToString());                                       
                    txtConDate.SetValue(DataBinder.Eval(DataItem, "cont_date", "{0:dd/MM/yyyy}").ToString());                     
                    txtConRefNo.Text = DataBinder.Eval(DataItem, "cont_ref_no").ToString();
                    
                    lblEmpSeq.Text = DataBinder.Eval(DataItem, "emp_seq").ToString();
                    txtEmployee.Text = DataBinder.Eval(DataItem, "full_name").ToString();

                    lblLoanSeq.Text = DataBinder.Eval(DataItem, "loan_seq").ToString();
                    txtLoanName.Text = DataBinder.Eval(DataItem, "loan_name").ToString();
                    txtLoan.Text = DataBinder.Eval(DataItem, "loan_seq").ToString();
                    txtLoanRefNo.Text = DataBinder.Eval(DataItem, "loan_ref_no").ToString();
                    txtAccNo.Text = DataBinder.Eval(DataItem, "loan_acc_no").ToString();
                    txtAmount.Text = DataBinder.Eval(DataItem, "cont_amount").ToString();

                    txtPeriod.Text = DataBinder.Eval(DataItem, "amt_period").ToString();
                    txtAmtPeriod.Text = DataBinder.Eval(DataItem, "pay_month").ToString();
                    radConStatus.SelectedValue = DataBinder.Eval(DataItem, "cont_status").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                                    
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }

           }

        private void BindRadio()
        {
            WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();
            DataTable dt;
            dt = obj.SelectConStatus();

            if (dt != null && dt.Rows.Count > 0)
            {
                radConStatus.Items.Clear();
                radConStatus.DataSource = dt;
                radConStatus.DataTextField = "cont_stat_name";
                radConStatus.DataValueField = "cont_stat_seq";
                radConStatus.DataBind();
                //   radStatus.SelectedIndex = 0;
            }
            else
            {
                radConStatus.Items.Clear();
            }


        }



        protected void Page_Load(object sender, EventArgs e)
        {

            
        }

 

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();

            int cont_seq, amt_period, emp_seq, loan_seq; ;
            string cont_ref_no, cont_date, cont_status, loan_ref_no, loan_acc_no, start_date, remarks;
            double cont_amount, pay_month;
            //decimal int_rate;

            //cont_seq = int.Parse(lblContSeq.Text);
            emp_seq = int.Parse(lblEmpSeq.Text);
            loan_seq = int.Parse(txtLoan.Text);
            amt_period = int.Parse(txtPeriod.Text);

            cont_ref_no = txtConRefNo.Text;

            if (obj.chkDupContRef(cont_ref_no) == true)
            {
                lblWarningDupConref.Visible = true;
                lblWarningDupConref.Text = "มีเลขที่สัญญานี้อยู่ในระบบแล้ว";
                return;
            }
            else
            {
                lblWarningDupConref.Visible = false;
            }

            cont_amount = Convert.ToDouble(txtAmount.Text);
            pay_month = Convert.ToDouble(txtAmtPeriod.Text);
            cont_status = radConStatus.SelectedValue;
            loan_ref_no = txtLoanRefNo.Text;
            loan_acc_no = txtAccNo.Text;
            cont_date = txtConDate.GetStringValue();
            start_date = txtStartDate.GetStringValue();
            remarks = txtRemarks.Text;          

            obj.insert(cont_ref_no, cont_date, emp_seq, loan_seq, cont_amount, cont_status, loan_ref_no, loan_acc_no
                , start_date, pay_month, amt_period, remarks, ssMng.EmpSeqI);            

        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();

            int cont_seq, amt_period, emp_seq,loan_seq;;
            string cont_ref_no,cont_date,cont_status,loan_ref_no, loan_acc_no,start_date,remarks;
            double cont_amount,pay_month;
            //decimal int_rate;

            cont_seq = int.Parse(lblContSeq.Text);
            emp_seq = int.Parse(lblEmpSeq.Text );
            loan_seq = int.Parse(lblLoanSeq.Text);
            amt_period=int.Parse(txtPeriod.Text);

            cont_ref_no = txtConRefNo.Text;

            if (obj.chkDupContRefEdit(cont_ref_no, cont_seq) == true)
            {
                lblWarningDupConref.Visible = true;
                lblWarningDupConref.Text = "มีเลขที่สัญญานี้อยู่ในระบบแล้ว";
                return;
            }
            else
            {
                lblWarningDupConref.Visible = false;

            }
            cont_amount = Convert.ToDouble(txtAmount.Text);
            pay_month =Convert.ToDouble(txtAmtPeriod.Text);
            cont_status = radConStatus.SelectedValue;   
            loan_ref_no = txtLoanRefNo.Text;
            loan_acc_no = txtAccNo.Text;
            cont_date = txtConDate.GetStringValue();
            start_date = txtStartDate.GetStringValue();
            remarks = txtRemarks.Text;
            
            obj.Update(cont_seq, cont_ref_no, cont_date, emp_seq, loan_seq, cont_amount, cont_status, loan_ref_no,loan_acc_no,  start_date,  pay_month,  amt_period, remarks, 1);
           
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
      
               
        protected void btnFindEmployee_Load(object sender, EventArgs e)
        {
            btnFindEmployee.Attributes.Add("OnClick", "LinkMDdetail(" + HideEmployee.ClientID + "," + "'Forms/DialogRC_Emp.aspx','../frmIframe.html',700,500)");
            
        }

        protected void btnFindLoan_Click(object sender, ImageClickEventArgs e)
        {

            txtLoanName.Text = "";


            string st = HideLoan.Value;
            if (st == "")
                return;


            lblLoanSeq.Text = st;

            int emp_seq = 0;
            if(lblEmpSeq.Text != "")
            {
             emp_seq = int.Parse(lblEmpSeq.Text);

            }
            WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();

            if (obj.chkDup(emp_seq, int.Parse(st)) == true)
            {
                lblWarningDup.Visible = true;
                lblWarningDup.Text = "มีรายชื่อนี้อยู่ในระบบแล้ว";
                return;

            }
            else
            {
                lblWarningDup.Visible = false;

            }

            DataTable dt;


            dt = obj.GetLoan(int.Parse(st));

            txtLoan.Text = dt.Rows[0]["loan_seq"].ToString();
            txtLoanName.Text = dt.Rows[0]["loan_name"].ToString();

        }

        protected void btnFindLoan_Load(object sender, EventArgs e)
        {
            btnFindLoan.Attributes.Add("OnClick", "LinkMDdetail(" + HideLoan.ClientID + "," + "'Forms/DialogWF_Loan.aspx','../frmIframe.html',700,500)");

        }

        protected void btnFindEmployee_Click1(object sender, ImageClickEventArgs e)
        {
            txtEmployee.Text = "";

            string st = HideEmployee.Value;
            if (st == "")
                return;

            lblEmpSeq.Text = st;



            WelfareLoanAgreementBLL obj = new WelfareLoanAgreementBLL();


            int loan_seq = 0;
            if (lblLoanSeq.Text != "")
            {
             loan_seq = int.Parse(lblLoanSeq.Text);
            }
            
            if (obj.chkDup(loan_seq, int.Parse(st)) == true)
            {
                lblWarningDup.Visible = true;
                lblWarningDup.Text = "มีรายชื่อนี้อยู่ในระบบแล้ว";
                return;

            }
            else
            {
                lblWarningDup.Visible = false;

            }

            DataTable dt;
            dt = obj.SelectEmpByID(int.Parse(st));
            txtEmployee.Text = dt.Rows[0]["full_name"].ToString();

        }




    


    }
}
        